//
//  ViewController.m
//  KaiXinDaDevice
//
//  Created by stonesMonkey on 2018/11/13.
//  Copyright © 2018年 stonesMonkey. All rights reserved.
//

#import "ViewController.h"
#import <KXDSafetyBox/KXDSafetyBox.h>
#import "ZYFTableViewCell.h"
#import "CardMessageVC.h"


@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,kxdiMateAppFaceDelegate>

@property (weak, nonatomic) IBOutlet UILabel *titleLabel;

@property (nonatomic, strong)KXDSafetyBox *manager;

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (strong, nonatomic) NSArray *dataSoures;
@property (weak, nonatomic) IBOutlet UIImageView *iconView;



@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 创建对象设置代理相当于初始化
    self.manager.delegate = self;

    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"ZYFTableViewCell" bundle:nil] forCellReuseIdentifier:@"ZYFTableViewCell"];
    if (self.manager.accessoryList.count > 0) {
        
        EAAccessory *accessory = self.manager.accessoryList[0];
        
        self.titleLabel.text = [NSString stringWithFormat:@"已链接%ld",accessory.connectionID];
        self.tableView.hidden = NO;
    } else {
        
        self.titleLabel.text = @"未连接设备";
        self.tableView.hidden = YES;
    }
    
    
}

// 懒加载
- (KXDSafetyBox *)manager {
    
    if (!_manager) {
        
        _manager = [KXDSafetyBox shareInstance];
        
    }
    return _manager;
}


#pragma mark - tableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSoures.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ZYFTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZYFTableViewCell" forIndexPath:indexPath];
    cell.cellData = self.dataSoures[indexPath.row];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"提示"
                                                                   message:self.dataSoures[indexPath.row]
                                                            preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault
                                                          handler:^(UIAlertAction * action) {
                                                              
                                                              [self runAction:indexPath];
                                                          }];
    
    UIAlertAction* defaultAction1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault
                                                          handler:^(UIAlertAction * action) {}];
    [alert addAction:defaultAction1];
    [alert addAction:defaultAction];
    
    [self presentViewController:alert animated:YES completion:nil];
    

    
}

- (void)runAction:(NSIndexPath *)indexPath {
    
    
    switch (indexPath.row) {
        case 0:
        {
            [kKXDSafetyBox scanQRCode:30];
            break;
        }
        case 1:
        {
            [kKXDSafetyBox readFingerPrintFeature:30];
            break;
        }
        case 2:
        {
            [kKXDSafetyBox readFingerPrintTemplate:30];
            break;
        }
        case 3:
        {
            //            [kKXDSafetyBox autoCheckCard];
            [kKXDSafetyBox readMagneticCard:60];
            break;
        }
        case 4:
        {
            [kKXDSafetyBox readIDCardInformation:60];
            break;
        }
        case 5:
        {
            [kKXDSafetyBox readIDCardPicture:3 timeout:30];
            break;
        }
        case 6:
        {
            [kKXDSafetyBox readICCardInformation:2 aid:@"" timeout:30];
            break;
        }
        case 7:
        {
             [kKXDSafetyBox readICCardInformation:4 aid:@"" timeout:30];
           
            break;
        }
        case 8:
        {
            [kKXDSafetyBox waitSwipeCardEvent:0x01 timeout:30];
            break;
        }
        case 9:
        {
            [kKXDSafetyBox exitIDCard];
            break;
        }
//        case 9:
//        {
//            [kKXDSafetyBox idCardExit];
//            break;
//        }
            
    }
}

//-(NSArray *)dataSoures{
//    if (!_dataSoures) {
//        _dataSoures = @[@"1.1 打开磁条卡",@"1.2 检查刷卡",@"1.3 取消刷卡",@"2.1 自动检卡",@"2.2 开启PBOC流程（IC卡复位只调用自动检卡）",@"2.3 获取55域（IC卡复位只调用自动检卡)", @"2.4取消读卡（IC卡复位只调用自动检卡）", @"3.1 打开扫描头", @"3.2 扫描头扫码 ",@"4.1 打开指纹模块",@"4.2 读取指纹模版",@"4.3 读取指纹特征",@"4.4 检测手指是否按下",@"5.1  打开二代证",@"5.2  扫描身份证",@"5.3 关闭二代证模块",@"5.4 退卡",@"磁条刷卡",@"读卡口读卡",@"ICRF感应卡",@"获取电池电量",@"获取ARQC",@"IC卡复位"];
//    }
//    return _dataSoures;
//}

-(NSArray *)dataSoures{
    if (!_dataSoures) {
        _dataSoures = @[@"scanQRCode",@"readFingerPrintFeature",@"readFingerPrintTemplate",@"readMagneticCard",@"readIDCardInformation",@"readIDCardPicture",@"readICCardInformation slo2 = 2",@"readICCardInformation slot = 4",@"waitSwipeCardEvent",@"exitIDCard"];
    }
    return _dataSoures;
}

#pragma mark - delegate
- (void)iMateDelegateSwipeCard:(NSInteger)returnCode track2:(NSString *)track2 track3:(NSString *)track3 error:(NSString *)error {
    
    CardMessageVC *vc = [CardMessageVC new];
    vc.view.backgroundColor = [UIColor whiteColor];
    vc.resultLabel.text = error;
    vc.oneTipLabel.text = [NSString stringWithFormat:@"%@:%@",@"二磁道数据",track2];
    
    vc.image1.image = nil;
    vc.image2.image = nil;
    vc.image3.image = nil;
    if (track3.length > 0) {
        
        vc.oneTipLabel.text = [NSString stringWithFormat:@"%@\n%@:%@",vc.oneTipLabel.text ,@"三磁道数据",track3];
    }
    
    
    
    if (returnCode == 0) {
        vc.resultLabel.text = @"刷卡成功";
    } else {
        
        vc.resultLabel.text = error;
    };
    
    
    NSLog(@"\n error = %@ \n returnCode = %ld \n track2 = %@ \n track3 = %@",error,(long)returnCode,track2,track3);
    
    [self.navigationController pushViewController:vc animated:YES];
    
}


- (void)iMateDelegateReadFeature:(NSInteger)returnCode feature:(NSString *)feature error:(NSString *)error {
    
    CardMessageVC *vc = [CardMessageVC new];
    vc.view.backgroundColor = [UIColor whiteColor];
    vc.resultLabel.text = error;
    vc.oneTipLabel.text = [NSString stringWithFormat:@"%@:%@",@"指纹特征值",feature];
    vc.image1.image = nil;
    vc.image2.image = nil;
    if (returnCode == 0) {
        vc.resultLabel.text = @"读取特征值成功";
    } else {
        
        vc.resultLabel.text = error;
    };
  [self.navigationController pushViewController:vc animated:YES];
}

- (void)iMateDelegateScanQRCode:(NSInteger)returnCode
                     qrCodeData:(NSString *)qrCodeData
                          error:(NSString *)error {
    
    CardMessageVC *vc = [CardMessageVC new];
    vc.view.backgroundColor = [UIColor whiteColor];
    vc.resultLabel.text = error;
    vc.oneTipLabel.text = [NSString stringWithFormat:@"%@:%@",@"二维码扫描",qrCodeData];
    
    vc.image1.image = nil;
    vc.image2.image = nil;
    if (returnCode == 0) {
        vc.resultLabel.text = error;
    } else {
        
        vc.resultLabel.text = error;
    };
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)iMateDelegateFingerPrintFeature:(NSInteger)returnCode
                               fpData1:(NSData *)fpData1
                               fpData2:(NSData *)fpData2
                                 error:(NSString *)error {
    
    CardMessageVC *vc = [CardMessageVC new];
    vc.view.backgroundColor = [UIColor whiteColor];
    vc.resultLabel.text = error;
    vc.oneTipLabel.text = [NSString stringWithFormat:@"%@:%@",@"指纹特征值",[[NSString alloc] initWithData:fpData1 encoding:NSUTF8StringEncoding] ];
    vc.image1.image = nil;
    vc.image2.image = nil;
    vc.resultLabel.text = error;
    
    [self.navigationController pushViewController:vc animated:YES];
}


/**
 *  读取指纹指纹模板
 *
 *  @param returnCode  返回状态 0 成功
 *  @param template     特征值
 *  @param error       错误信息
 */
-(void)iMateDelegateFingerPrintTemplate:(NSInteger)returnCode
                               template:(NSData *)template error:(NSString *)error {
    
    
    CardMessageVC *vc = [CardMessageVC new];
    vc.view.backgroundColor = [UIColor whiteColor];
    vc.resultLabel.text = error;
    vc.oneTipLabel.text = [NSString stringWithFormat:@"%@:%@",@"指纹模板",[[NSString alloc] initWithData:template encoding:NSUTF8StringEncoding]];
    
    vc.image1.image = nil;
    vc.image2.image = nil;
    vc.resultLabel.text = error;
    
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)iMateDelegateIDReadMessage:(NSInteger)returnCode
                       information:(NSData *)information
                             error:(NSString *)error {
    
    CardMessageVC *vc = [CardMessageVC new];
    vc.view.backgroundColor = [UIColor whiteColor];
    vc.resultLabel.text = error;
    
    vc.image1.image = nil;
    vc.image2.image = nil;
    
    vc.oneTipLabel.text = [NSString stringWithFormat:@"%@:%@", @"身份证信息",[[NSString alloc] initWithData:information encoding:NSUTF8StringEncoding]];
    
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)iMateDelegateIDCardPicture:(NSInteger)returnCode headPhoto:(NSData *)photo nationalEmblemPhoto:(NSData *)NEphoto identityWitness:(NSData *)IWphoto error:(NSString *)err {
    
    CardMessageVC *vc = [CardMessageVC new];
    vc.view.backgroundColor = [UIColor whiteColor];
    vc.resultLabel.text = err;
    
    vc.image1.image = [UIImage imageWithData:photo];
    vc.image2.image = [UIImage imageWithData:NEphoto];
    vc.image3.image = [UIImage imageWithData:IWphoto];
    vc.oneTipLabel.text = @"";
    
    [self.navigationController pushViewController:vc animated:YES];
    
}

- (void)iMateDelegateWaitEvent:(NSInteger)returnCode
                       eventId:(NSInteger)eventId
                          data:(NSData *)data
                         error:(NSString *)error {
    
    CardMessageVC *vc = [CardMessageVC new];
    vc.view.backgroundColor = [UIColor whiteColor];
    vc.resultLabel.text = error;
    
    vc.image1.image = nil;
    vc.image2.image = nil;
    vc.image3.image = nil;
    
    vc.oneTipLabel.text = [NSString stringWithFormat:@"%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]];
    
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)iMateDelegateICCardInfo:(NSInteger)returnCode information:(NSString *)information error:(NSString *)error {
    
    CardMessageVC *vc = [CardMessageVC new];
    vc.view.backgroundColor = [UIColor whiteColor];
    vc.resultLabel.text = error;
    NSString *keyStr = @"IC卡复位信息|卡号|卡序号|等效二磁track2|aid|55域|脱机交易信息";
    NSArray *keyArr = [keyStr componentsSeparatedByString:@"|"];
    NSString *valueStr = information;
    NSArray *valueArr = [valueStr componentsSeparatedByString:@"|"];
    NSMutableString *showStr= [[NSMutableString alloc] init];
    for (NSInteger index = 0; index <keyArr.count; index++) {
        
        NSString *key = keyArr[index];
        NSString *value = @"";
        if (index < valueArr.count) {
            value = valueArr[index];
        }
    
        [showStr appendString:[NSString stringWithFormat:@"%@:%@\n",key,value]];
    }
    vc.oneTipLabel.text = showStr;
    vc.image1.image = nil;
    vc.image2.image = nil;
    vc.resultLabel.text = error;
    
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)iMateDelegateConnectStatus:(BOOL)isConnecting {
    
    NSLog(@"%@",isConnecting == YES?@"已链接":@"断开链接");
    if (isConnecting) {
        
        EAAccessory *accessory = self.manager.accessoryList[0];
        self.titleLabel.text = [NSString stringWithFormat:@"已链接%ld",accessory.connectionID];
        self.tableView.hidden = NO;
    } else {
        
        self.titleLabel.text = @"未连接设备";
        self.tableView.hidden = YES;
    }
}



// 超时
- (void)iMateDelegateNoResponse:(NSString *)error {
    
    NSLog(@"%@",error);
}

- (void)iMateDelegateModuleStatus:(BOOL)isWorking {
    
    NSLog(@"%@",isWorking?@"设备正常":@"设备异常");

}

























@end
